<template>
    <div v-if="visible" class="blm-source-detail" @click.self="close">
        <div class="blm-source-detail-container">
            <div class="blm-source-detail-header">
                <div class="blm-source-detail-title">来源</div>
                <div class="blm-source-detail-close">
                    <img
                     style="width:18px"
                        :src="require('@/assets/images/close.png')"
                        alt=""
                        @click.stop="close"
                    />
                </div>
            </div>
            <div class="blm-source-detail-url" v-if="sourceUrlList.length > 0">
                <div
                    class="blm-source-detail-url-item"
                    v-for="item in sourceUrlList"
                    @click="jumperToDetail(item)"
                >
                    <p class="blm-source-detail-url-title">{{ item.title }}</p>
                    <p class="blm-source-detail-url-value" v-if="item.url">
                        {{ item.url }}
                    </p>
                    <p v-if="item.summary">{{ item.summary }}</p>
                </div>
            </div>
            <div class="blm-source-detail-content" v-else>
                <p class="blm-source-detail-content-title">{{ sourceData.title }}</p>
                <jgy-render-sdk
                    v-if="sourceDetail"
                    statName="yyzs"
                    :answer="sourceDetail"
                    sourceType="Iwencai"
                    language="zh"
                    :jumpFun="jgyJumpFun"
                />
                <div v-else class="blm-source-detail-loading">加载中...</div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        sourceData: {
            type: Object | Array,
            default() {
                return {};
            }
        },
        visible: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            sourceDetail: null,
            sourceUrlList: []
        };
    },
    created() {},
    watch: {
        visible(n) {
            if (n) {
                if (Array.isArray(this.sourceData)) {
                    this.sourceUrlList = this.sourceData;
                } else {
                    this.openSourceDetail(this.sourceData);
                }
            } else {
                this.sourceDetail = null;
                this.sourceUrlList = [];
            }
        }
    },
    methods: {
        openSourceDetail(item) {
            const params = {
                query: item.title,
                query_type: item.query_type,
                source: 'b_customer_trail_04'
            };
            if(item.trace_data){
                params.condition=item.trace_data
            }else{
                params.model_sql=item.model_sql
            }
            const url = "/saas/robot/ai/source/gateway/render/getData";
            fetch(url, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(params)
            })
                .then(response => response.json())
                .then(data => {
                    console.log(data.answer);
                    this.sourceDetail = data.answer;
                })
                .catch(error => console.error("Error:", error));
        },
        close() {
            this.$emit("update:visible", false);
        },
        jgyJumpFun(v) {
            console.log(v);
        },
        jumperToDetail(sourceData) {
            if (sourceData.url) {
                window.open(sourceData.url);
            }
        }
    }
};
</script>
<style scoped lang="less">
@import (once) '@/styles/variable.less';
.blm-source-detail {
    width: 100%;
    position: fixed;
    inset: 0;
    background: @black-color-a-45;
    z-index: 123;
}
.blm-source-detail-container {
    background: @white-color-4;
    position: absolute;
    bottom: 0;
    top: 100px;
    padding: 12px;
    width: 100%;
    box-sizing: border-box;
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
}
.blm-source-detail-content {
    background: @white-color-1;
    padding: 6px;
}
.blm-source-detail-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 12px;
}
.blm-source-detail-content /deep/ .chunk-title {
    display: none;
}
.blm-source-detail-content-title {
    margin-bottom: -12px;
}
.blm-source-detail-loading {
    height: 54px;
    line-height: 54px;
    text-align: center;
    color: @black-color-13;
}
.blm-source-detail-url-title {
    color: @black-color-9;
}
.blm-source-detail-url-value {
    color: @black-color-13;
    font-size: 12px;
    white-space: nowrap; /* 防止文本换行 */
    overflow: hidden; /* 隐藏溢出的文本 */
    text-overflow: ellipsis;
}
.blm-source-detail-url-item {
    background: @white-color-1;
    padding: 12px;
    margin-bottom: 8px;
    border-radius: 4xp;
}
.blm-source-detail-content,.blm-source-detail-url {
    max-height: calc(100vh - 100px);
    overflow-y: auto;
}
</style>
